<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function procmsg($topic, $msg)
{
    // 输出消息
    echo "Msg Recieved: $msg" . PHP_EOL;
}

if (!function_exists('middleware')) {
    /**
     * 实例化验证器
     * @param string $name 验证器名称
     * @param string $layer 业务层名称
     * @param bool $appendSuffix 是否添加类名后缀
     * @param string $common
     * @return \think\Validate
     */
    function middleware($name = '', $layer = 'middleware', $appendSuffix = false, $common = "common")
    {
        return \think\Loader::validate($name, $layer, $appendSuffix, $common);
    }
}

if (!function_exists('get_client_ip')) {
    function get_client_ip($type = 0, $adv = false)
    {
        $type = $type ? 1 : 0;
        static $ip = NULL;
        if ($ip !== NULL) return $ip[$type];
        if ($adv) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos = array_search('unknown', $arr);
                if (false !== $pos) unset($arr[$pos]);
                $ip = trim($arr[0]);
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u", ip2long($ip));
        $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }
}

if (!function_exists('array_sort')) {
    function array_sort($array, $sortRule = "", $order = "asc")
    {
        /**
         * $array = [
         *              ["book"=>10,"version"=>10],
         *              ["book"=>19,"version"=>30],
         *              ["book"=>10,"version"=>30],
         *              ["book"=>19,"version"=>10],
         *              ["book"=>10,"version"=>20],
         *              ["book"=>19,"version"=>20]
         *      ];
         */
        if (is_array($sortRule)) {
            /**
             * $sortRule = ['book'=>"asc",'version'=>"asc"];
             */
            usort($array, function ($a, $b) use ($sortRule) {
                foreach ($sortRule as $sortKey => $order) {
                    if ($a[$sortKey] == $b[$sortKey]) {
                        continue;
                    }
                    return (($order == 'desc') ? -1 : 1) * (($a[$sortKey] < $b[$sortKey]) ? -1 : 1);
                }
                return 0;
            });
        } else if (is_string($sortRule) && !empty($sortRule)) {
            /**
             * $sortRule = "book";
             * $order = "asc";
             */
            usort($array, function ($a, $b) use ($sortRule, $order) {
                if ($a[$sortRule] == $b[$sortRule]) {
                    return 0;
                }
                return (($order == 'desc') ? -1 : 1) * (($a[$sortRule] < $b[$sortRule]) ? -1 : 1);
            });
        } else {
            usort($array, function ($a, $b) use ($order) {
                if ($a == $b) {
                    return 0;
                }
                return (($order == 'desc') ? -1 : 1) * (($a < $b) ? -1 : 1);
            });
        }
        return $array;
    }
}

if (!function_exists('tpr_infinite_tree')) {
    /**
     * 无限层级的生成树方法
     * @param $data
     * @param string $parent_index
     * @param string $data_index
     * @param string $child_name
     * @return array|bool
     */
    function tpr_infinite_tree($data, $parent_index = 'parent_id', $data_index = 'id', $child_name = 'child')
    {
        $items = [];
        foreach ($data as $d) {
            $items[$d[$data_index]] = $d;
            if (!isset($d[$parent_index]) || !isset($d[$data_index]) || isset($d[$child_name])) {
                return false;
            }
        }
        $tree = [];
        $n = 0;
        foreach ($items as $item) {
            if (isset($items[$item[$parent_index]])) {
                $items[$item[$parent_index]][$child_name][] = &$items[$item[$data_index]];
            } else {
                $tree[$n++] = &$items[$item[$data_index]];
            }
        }
        return $tree;
    }
}

if (!function_exists('traversal_tree_to_node_list')) {
    /**
     * 遍历生成树，生成节点列表
     * @param $tree
     * @param array $data
     * @param int $layer
     * @param string $layer_name
     * @param string $child_name
     */
    function traversal_tree_to_node_list($tree, &$data = [], $layer = 0, $layer_name = 'layer', $child_name = 'child')
    {
        foreach ($tree as $t) {
            $node = $t;
            unset($node[$child_name]);
            $node[$layer_name] = $layer;
            $data[] = $node;
            if (isset($t[$child_name]) && !empty($t[$child_name])) {
                traversal_tree_to_node_list($t[$child_name], $data, $layer + 1);
            }
        }
    }
}

if (!function_exists('check_data_to_string')) {
    function check_data_to_string(&$array = [])
    {
        if (is_array($array)) {
            foreach ($array as &$a) {
                if (is_array($a)) {
                    $a = check_data_to_string($a);
                }
                if (is_int($a)) {
                    $a = strval($a);
                }
                if (is_null($a)) {
                    $a = "";
                }
            }
        } else if (is_int($array)) {
            $array = strval($array);
        } else if (is_null($array)) {
            $array = "";
        }
        return $array;
    }
}

if (!function_exists('object_to_array')) {
    function object_to_array($object)
    {
        $object = json_decode(json_encode($object), true);
        return $object;
    }
}

if (!function_exists('check_sign')) {
    function check_sign($post_timestamp, $post_sign)
    {
        $sign = md5($post_timestamp . "tpr");
        return $post_sign != $sign ? $sign : true;
    }
}

if (!function_exists('p')) {
    function p()
    {
        $args = func_get_args();
        echo "<pre>";
        print_r($args);
        echo "</pre>";
    }
}
if (!function_exists('fly_curl')) {
    function fly_curl($url, $postFields = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FAILONERROR, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        // curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:"));
        if (!empty($postFields)) {

            $postBodyString = "";
            $postMultipart = false;

            if (is_array($postFields) && 0 < count($postFields)) {
                foreach ($postFields as $k => $v) {
                    if ("@" != substr($v, 0, 1))                 // 判断是不是文件上传
                    {
                        $postBodyString .= "$k=" . urlencode($v) . "&";
                    } else                 // 文件上传用multipart/form-data，否则用www-form-urlencoded
                    {
                        $postMultipart = true;
                    }
                }
                unset($k, $v);
            } else {
                $postBodyString = $postFields . "&";
            }
            curl_setopt($ch, CURLOPT_POST, 1);
            if ($postMultipart) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
            } else {
                // var_dump($postBodyString);
                curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
            }
        }
        $reponse = curl_exec($ch);
        // echo "<pre>";
        // var_dump(curl_getinfo($ch));
        // return curl_getinfo($ch);
        if (curl_errno($ch)) {
            throw new Exception(curl_error($ch), 0);
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                throw new Exception($reponse, $httpStatusCode);
            }
        }
        curl_close($ch);
        return $reponse;
    }
}

if (!function_exists('curl_get')) {

    function curl_get($url, $timeout = 10)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);       //连接时间
        // 关闭SSL验证
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        $res = curl_exec($curl);     //返回api的json对象
        if (curl_errno($curl)) {
            return curl_error($curl);
        }

        curl_close($curl);
        return $res;    //返回json对象
    }
}

if (!function_exists('curl_post')) {
    function curl_post($url, $data, $type = 1)
    {

        if ($type) {
            $data = http_build_query($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_HEADER, false);      //不直接输出
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);       //连接时间
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }

        curl_close($curl);
        if ($type == 1) {
            return $data;
        } else {
            return json_decode($data, 1);
        }
    }
}

if (!function_exists('httpsRequest')) {
    function httpsRequest($url, $data, $type)
    {
        if ($type == 'json') {
            $headers = [
                "Content-type: application/json;charset=UTF-8",
                "Accept: application/json",
                "Cache-Control: no-cache",
                "Pragma: no-cache"
            ];
            $data = json_encode($data);
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($curl);

        curl_close($curl);
        return $output;
    }
}
